package br.com.alugargaragem.ui.stripes.interceptor;

import static br.com.lambdacore.validator.Validator.is;

import java.util.Arrays;
import java.util.List;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.controller.ExecutionContext;
import net.sourceforge.stripes.controller.Interceptor;
import net.sourceforge.stripes.controller.Intercepts;
import net.sourceforge.stripes.controller.LifecycleStage;
import br.com.alugargaragem.ui.action.HomeAction;
import br.com.alugargaragem.ui.action.SearchGarageAction;
import br.com.alugargaragem.ui.action.LoginAction;
import br.com.alugargaragem.ui.stripes.action.AlugaGaragemActionBean;
import br.com.alugargaragem.ui.stripes.action.AlugaGaragemActionBeanContext;
import br.com.lambdacore.ui.stripes.action.LambdaCoreActionBean;

@Intercepts(LifecycleStage.ActionBeanResolution)
public class LoginInterceptor implements Interceptor {

	@SuppressWarnings("unchecked")
	private static final List<Class<? extends AlugaGaragemActionBean>> ALLOW = Arrays
			.asList(LoginAction.class, HomeAction.class,
					SearchGarageAction.class);

	public Resolution intercept(ExecutionContext executionContext)
			throws Exception {
		Resolution resolution = executionContext.proceed();
		AlugaGaragemActionBeanContext actionBeanContext = (AlugaGaragemActionBeanContext) executionContext
				.getActionBeanContext();
		LambdaCoreActionBean actionBean = (LambdaCoreActionBean) executionContext
				.getActionBean();
		Class<? extends ActionBean> cls = actionBean.getClass();
		if (actionBeanContext.getLoginData() == null
				&& is(ALLOW).notContains(cls)) {
			resolution = new RedirectResolution(HomeAction.class);
		}
		return resolution;
	}
}
